﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ciripescu_WebRole.BL;
using Ciripescu_WebRole.Entities;

namespace Ciripescu_WebRole
{
    public partial class sendmessage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {     
            
            if (!IsPostBack && Request.UrlReferrer != null)
                ViewState.Add("ref", Request.UrlReferrer.ToString());
            try
            {
                // verificam daca userul exista - nu vrem probleme de securitate si XSS-uri!
                if (Request.QueryString["userName"] == null)
                {
                    errors.InnerHtml = "User invalid";
                    return;
                }
                string rcptUser = Request.QueryString["userName"];
                UserManager um = new UserManager();
                User usr = um.GetItem(rcptUser);
                //lblTitlu.Text = "Trimite mesaj privat la <a href='/profile.aspx?userName=" + rcptUser + "' class='mylink'>" + rcptUser + "</a>:";
                lblTitlu.Text = "Trimite mesaj privat la <a href='/u/" + rcptUser + "/' class='mylink'>" + rcptUser + "</a>:";
            }
            catch (Exception ex)
            {
                errors.InnerHtml = "Eroare interna. Va rugam incercati mai tarziu!";
                return;
            }
        }

        protected void btnSendMessage_Click(object sender, EventArgs e)
        {
            if (Request.QueryString["userName"] == null)
            {
                errors.InnerHtml = "User invalid";
                return;
            }
            string rcptUser = Request.QueryString["userName"];
            try
            {
                UserManager um = new UserManager();
                User usr = um.GetItem(rcptUser);
                if (um == null)
                {
                    errors.InnerHtml = "User invalid";
                    return;
                }

                if (txtMesaj.Text.Length > 255)
                {
                    errors.InnerHtml = "Textul introdus este prea lung (mai mare de 255 caractere).<br>";
                    return;
                }
            
                CiripMessage c = new CiripMessage((string)Session["UserName"], rcptUser, txtMesaj.Text, 0, string.Empty);
                CiripMessage outboxCopy = new CiripMessage(rcptUser, (string)Session["UserName"], txtMesaj.Text, 2, string.Empty);
                CiripMessageManager msgManager = new CiripMessageManager();
                msgManager.Insert(c);
                msgManager.Insert(outboxCopy);
                errors.InnerHtml = "Mesajul a fost trimis!";
                txtMesaj.Text = "";
            }
            catch (Exception ex)
            {
                errors.InnerHtml = "Eroare interna. Va rugam incercati mai tarziu!";
                return;
            }
            
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            if (ViewState["ref"] != null)
                Response.Redirect((string)ViewState["ref"]);
        }
    }
}
